﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.BLL;

namespace LEARNREN.COM.UI.LabelProviders
{
    /// <summary>
    /// 分类列表标签
    /// </summary>
    class CTLLabel : AbstractLabelProvider
    {
        const string LISTFORMAT = "<li class=\"ctl_li{2}\"><a href=\"{0}\" target=\"_self\">{1}</a></li>";

        public CTLLabel(string label)
            : base(label)
        {

        }

        public override string GetHtml()
        {
            try
            {

                string pcidStr = LabelProviderHelper.GetProperties(this.LabelString)[0];

                long pcid = 0;
                if (pcidStr.ToLower() == "cur")
                {
                    pcid = this.Context.CatalogueID;
                }
                else
                {
                    pcid = int.Parse(pcidStr);
                }

                var clist = from d in BasicCache.AllCatalogueList where d.ParentId == pcid orderby d.Order ascending select d;
                int count = clist.Count();
                if (count == 0)
                {
                    return string.Empty;
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<ul class=\"ctl\">");
                    int i = 0;
                    foreach (var catalogue in clist)
                    {
                        if (i == count - 1)
                        {
                            if (BasicCache.SiteConfig.IsStaticEnable)
                            {
                                sb.AppendLine(string.Format(LISTFORMAT, catalogue.HtmlPath, catalogue.Title," ctl_last"));
                            }
                            else
                            {
                                sb.AppendLine(string.Format(LISTFORMAT,
                                                            System.Web.VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}", catalogue.CatalogueType, catalogue.Id)),
                                                            catalogue.Title,
                                                            " ctl_last"));
                            }
                        }
                        else
                        {
                            if (BasicCache.SiteConfig.IsStaticEnable)
                            {
                                sb.AppendLine(string.Format(LISTFORMAT, catalogue.HtmlPath, catalogue.Title,string.Empty));
                            }
                            else
                            {
                                sb.AppendLine(string.Format(LISTFORMAT,
                                                            System.Web.VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}", catalogue.CatalogueType, catalogue.Id)),
                                                            catalogue.Title, string.Empty));
                            }
                        }

                        i++;
                    }

                    sb.AppendLine("</ul>");
                    return sb.ToString();
                }
            }
            catch
            {
                return this.LabelString;
            }
        }
    }
}
